<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="作者id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入作者名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片id" prop="picId">
        <el-input
          v-model="queryParams.picId"
          placeholder="请输入图片id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="页码" prop="picPage">
        <el-input
          v-model="queryParams.picPage"
          placeholder="请输入页码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="picTitle">
        <el-input
          v-model="queryParams.picTitle"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="picStatus">
        <el-select v-model="queryParams.picStatus" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in picStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="R18" prop="picR18">
        <el-select v-model="queryParams.picR18" placeholder="请选择R18" clearable size="small">
          <el-option
            v-for="dict in picR18Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="picTags">
        <el-input
          v-model="queryParams.picTags"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['entity:image:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="imageList" highlight-current-row height="680">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="Pid"><span>{{ props.row.picId }}</span></el-form-item>
            <el-form-item label="Uid"><span>{{ props.row.userId }}</span></el-form-item>
            <el-form-item label="Page"><span>{{ props.row.picPage }}</span></el-form-item>
            <el-form-item label="Width"><span>{{ props.row.picWidth }}</span></el-form-item>
            <el-form-item label="Height"><span>{{ props.row.picHeight }}</span></el-form-item>
            <el-form-item label="Tags"><span>{{ props.row.picTags }}</span></el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="Author" align="center" prop="userName"/>
      <el-table-column label="Title" align="center" prop="picTitle"/>
      <el-table-column label="Image" align="center" prop="picPath">
        <template slot-scope="scope">
          <div>
            <el-image style="height:100px;" :src="urlPre+scope.row.picPath"
                      :preview-src-list="imageList.map((item) => urlPre+item.picPath)"
                      fit="contain"></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="R18" align="center" prop="picR18" :formatter="picR18Format"/>
      <el-table-column label="Status" align="center" prop="picStatus" :formatter="picStatusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['entity:image:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['entity:image:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="Uid" prop="userId">
              <el-input v-model="form.userId" placeholder="Uid" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Author" prop="userName">
              <el-input v-model="form.userName" placeholder="Author" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="Pid" prop="picId">
              <el-input v-model="form.picId" placeholder="Pid" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Page" prop="picPage">
              <el-input v-model="form.picPage" placeholder="Page" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="Url" prop="picUrl">
              <el-input v-model="form.picUrl" placeholder="Url" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Mas" prop="picMas">
              <el-input v-model="form.picMas" placeholder="Mas" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="Title" prop="picTitle">
          <el-input v-model="form.picTitle" placeholder="Title" readonly/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="View" prop="picPath">
              <el-image :src="urlPre+form.picPath" style="height:100px;"
                        :preview-src-list="[urlPre+form.picPath]" fit="contain"></el-image>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Path" prop="picPath">
              <el-input v-model="form.picPath" placeholder="Path" readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="Status" prop="picStatus">
              <el-select v-model="form.picStatus" placeholder="Status" disabled>
                <el-option
                  v-for="dict in picStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="R18" prop="picR18">
              <el-select v-model="form.picR18" placeholder="R18" disabled>
                <el-option
                  v-for="dict in picR18Options"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="Width" prop="picWidth">
              <el-input v-model="form.picWidth" placeholder="Width" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Height" prop="picHeight">
              <el-input v-model="form.picHeight" plasceholder="Height" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="Tags" prop="picTags">
          <el-input v-model="form.picTags" placeholder="Tags" type="textarea" autosize readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listImage, getImage, delImage, addImage, updateImage, exportImage} from "@/api/entity/image";

  export default {
    name: "Image",
    data() {
      return {
        urlPre: process.env.VUE_APP_BASE_API,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 图片表格数据
        imageList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        picStatusOptions: [],
        // R18字典
        picR18Options: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: null,
          userName: null,
          picId: null,
          picPage: null,
          picTitle: null,
          picUrl: null,
          picMas: null,
          picPath: null,
          picStatus: null,
          picR18: null,
          picWidth: null,
          picHeight: null,
          picTags: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("e_status").then(response => {
        this.picStatusOptions = response.data;
      });
      this.getDicts("e_yesno").then(response => {
        this.picR18Options = response.data;
      });
    },
    methods: {
      /** 查询图片列表 */
      getList() {
        this.loading = true;
        listImage(this.queryParams).then(response => {
          this.imageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      picStatusFormat(row, column) {
        return this.selectDictLabel(this.picStatusOptions, row.picStatus);
      },
      // R18字典翻译
      picR18Format(row, column) {
        return this.selectDictLabel(this.picR18Options, row.picR18);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: null,
          userName: null,
          picId: null,
          picPage: null,
          picTitle: null,
          picUrl: null,
          picMas: null,
          picPath: null,
          picStatus: 0,
          picR18: 0,
          picWidth: null,
          picHeight: null,
          picTags: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加图片";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getImage(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改图片";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateImage(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addImage(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除图片编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delImage(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有图片数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportImage(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
